package com.getpebble.android.core.auth.activity;

import android.accounts.AccountAuthenticatorActivity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.Toast;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.framework.fragment.IHandleBackPress;
import com.getpebble.android.core.PblBaseActivity;
import com.getpebble.android.core.auth.fragment.LoginFragment;
import com.getpebble.android.core.auth.fragment.LoginWebViewFragment;
import com.getpebble.android.main.sections.appstore.fragment.NoConnectivityFragment;
import com.getpebble.android.onboarding.fragment.PebbleOnlyFragment;

public class LoginActivity extends AccountAuthenticatorActivity {
    protected String TAG = PblBaseActivity.class.getSimpleName();
    private FragmentManager mFragmentManager = null;

    public void onCreate(Bundle savedInstanceState) {
        if (PebbleApplication.getSessionManager().fetchUserAccount() != null) {
            Toast.makeText(this, getString(R.string.login_more_than_one_account), 1).show();
            finish();
        }
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        this.mFragmentManager = getFragmentManager();
        setContentView(R.layout.activity_pbl_base);
        switchFragment(new PebbleOnlyFragment());
    }

    public void switchToLoginFragment() {
        switchFragment(new LoginFragment());
    }

    public void switchToLoginWebFragment(String url) {
        LoginWebViewFragment fragment = new LoginWebViewFragment();
        Bundle arguments = new Bundle();
        arguments.putString("login_web_view_url", url);
        fragment.setArguments(arguments);
        switchFragment(fragment, false, false, true);
    }

    public void switchToNoConnectivityFragment(Fragment previousFragment) {
        switchFragment(new NoConnectivityFragment(), false, false, true);
    }

    protected final void switchFragment(Fragment fragment) {
        Trace.verbose(this.TAG, "switchFragment(" + fragment + ")");
        switchFragment(fragment, false, false, false);
    }

    protected final void switchFragment(Fragment fragment, boolean allowSameFragment, boolean force, boolean addToBackStack) {
        Trace.verbose(this.TAG, "switchFragment(" + fragment + ", " + allowSameFragment + ", " + addToBackStack + ")");
        Fragment currentFragment = getCurrentFragment();
        if (allowSameFragment || currentFragment == null || !currentFragment.getClass().getName().equals(fragment.getClass().getName())) {
            FragmentTransaction transaction = this.mFragmentManager.beginTransaction();
            transaction.replace(R.id.grp_fragment_container, fragment);
            if (addToBackStack && transaction.isAddToBackStackAllowed()) {
                transaction.addToBackStack(fragment.getClass().getName());
            }
            transaction.commit();
            if (force) {
                this.mFragmentManager.executePendingTransactions();
                return;
            }
            return;
        }
        Trace.info(this.TAG, "Cannot switch, same fragment and 'allowSameFragment' is false!");
    }

    public final Fragment getCurrentFragment() {
        Fragment fragment = this.mFragmentManager != null ? this.mFragmentManager.findFragmentById(R.id.grp_fragment_container) : null;
        Trace.verbose(this.TAG, "getCurrentFragment()::Fragment[" + fragment + "]");
        return fragment;
    }

    public void onBackPressed() {
        Fragment currentFragment = getCurrentFragment();
        if (currentFragment != null && (currentFragment instanceof IHandleBackPress) && ((IHandleBackPress) currentFragment).canGoBack()) {
            ((IHandleBackPress) currentFragment).goBack();
        } else {
            super.onBackPressed();
        }
    }
}
